// pages/tosendorder/tosendorder.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    menu:[],
    goods: [],
    multiArray: [],
    goods1:[],
   

    index: 0,
    multiIndex: [0, 0],

    img0: '../../img/20181201173806.png', 
    img1:'../../img/20181201173806.png',
    img2:'../../img/20181201173806.png',
    x:'',
    
    remark: '',
    goodsid: '',
    addr_id: '',
    date: '',
    userid: '',
    img:[],
  },
  bindPickerChange(e) {
   
    var that =this;
    console.log(that.data.goods[e.detail.value[0]][e.detail.value[1]]['id'])
    that.setData({
      goodsid: that.data.goods[e.detail.value[0]][e.detail.value[1]]['id']
    })
  },
  lala(e) {
    console.log('修改的列为', e.detail.column, '，值为', e.detail.value);
    var data = {
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex
    };
    data.multiIndex[e.detail.column] = e.detail.value;
    if (e.detail.column == 0) {
      console.log('picker发送选择改变，携带值为', e.detail.value)
      data.multiArray[1] = this.data.goods1[e.detail.value];
      data.multiIndex[1] = 0;
      data.multiIndex[2] = 0;
      this.setData(data);
    }
    
    
  },
  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.request({
      url: 'https://wukongweixiu.cn/index/order/getmenu',
      success: function (res) {
        console.log(res)
        if (res.data.code == 1) {
          that.setData({
            menu: res.data.data.menu,
            goods: res.data.data.goods,
            goods1: res.data.data.goods1,
            multiArray: [res.data.data.menu, res.data.data.goods1[0]],
            goodsid: res.data.data.goods[0][0]['id']
          })
        }
      }
    })
    wx.request({
      url: 'https://wukongweixiu.cn/index/Defaults/getuserinfo',
      data:{
        id: getApp().globalData.id,
      },
      success: function (res) {
        console.log(res)
        if (res.data.code == 1) {
          that.setData({
            addr_id: res.data.data.addr.addr_id,
          })
        }
      }
    })
  },

  onUnload: function (option) {
      var _this = this;
      _this.setData({
        userid: getApp().globalData.id,
      })
  },
  bindDateChange: function (e) {
    // console.log(e.detail.value)
    var that = this
    that.setData({
      date: e.detail.value
    })

  },
  //提交数据到服务器
  addCard:function(){
    var _this = this;
    var data={
      remark: _this.data.remark,
      goodsid: _this.data.goodsid,
      addr_id: _this.data.addr_id,
      sevrice_time: _this.data.date,
      userid: getApp().globalData.id,
      img: _this.data.img
    }
    wx.request({
      url: 'https://wukongweixiu.cn/index/order/todosenorder',
      data:data,
      success:function(res){
        if(res.data.code == 1){
          wx.showToast({
            title: '已提交成功！',
            duration: 3000
          });
          var data = {
            order_mun: res.data.data.order_mun,
            totalPrice: 10,
            type:1

          }
          wx.navigateTo({
            url: '../cashierDesk/cashierDesk?data=' + JSON.stringify(data),
          });
        }
      }
    })
  },
  remark:function(e){
    var _this = this;
    _this.setData({
      remark: e.detail.value
    })
  },

  chooseImageTap: function (e) {
    let _this = this;
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#f7982a",
      success: function (res) {
        var x = e.target.dataset.id;
        
        _this.setData({
          x:x,
        })
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            _this.chooseWxImage('album')
          } else if (res.tapIndex == 1) {
            _this.chooseWxImage('camera')
          }
        }
      }
    })
  },
  chooseWxImage: function (type) {
    let _this = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: [type],
      success: function (res) {
        wx.uploadFile({
          url: 'https://wukongweixiu.cn/index/order/uploadImg',
          filePath: res.tempFilePaths[0],
          name:'file',
          success: function (re) {
            var jsonStr = re.data;
            jsonStr = jsonStr.replace(" ", "");
            if (typeof jsonStr != 'object') {
              jsonStr = jsonStr.replace(/\ufeff/g, "");//重点
              var jj = JSON.parse(jsonStr);
            }
            var x = _this.data.x;
            var img = _this.data.img;
            img[x] = jj.data.src;
            if (jj.code == 1) {
              if (x == 0) {
                _this.setData({
                  img0: 'https://wukongweixiu.cn' + jj.data.src,
                  img
                })
              } else if(x == 1){
                _this.setData({
                  img1: 'https://wukongweixiu.cn' + jj.data.src,
                  img
                })
              }else{
                _this.setData({
                  x: 'https://wukongweixiu.cn' + jj.data.src,
                  img
                })
              }
              wx.showToast({
                title: '已提交发布！',
                duration: 3000
              });
            }
          }
        })
      }
    })
  },
  addr:function(e){
    wx.navigateTo({
      url: '../addressList/addressList',
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})